package com.netease.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.cors.CorsUtils;

import com.netease.jichu.Product;
import com.netease.jichu.Trx;
import com.netease.jichu.User;
import com.netease.service.ProductService;
import com.netease.service.TrxService;

@Controller
public class TrxController {
//显示意购买商品的内容
	@Autowired
	private TrxService ts;
	@Autowired
	private ProductService ps;
	
	@RequestMapping(value="/account",method=RequestMethod.GET)
	public String buyList(Model model,Product product){
		//账务页面
		List<Trx> trx=ts.buyList();
		for(Trx tr: trx){
			System.out.println(tr.getImage()+tr.getNumber()+tr.getTitle());
		}
		model.addAttribute("buyList",trx);
		return"account";
	}
	@RequestMapping(value="/api/buy",method=RequestMethod.POST)
	public void insertTrx(HttpSession session,Model model,@RequestBody List<Trx> T){
		User user = (User) session.getAttribute("user");;
		Trx buyertrx=new Trx();
		Product product=new Product();
		for(Trx tx:T){
			buyertrx.setContentId(tx.getId());
			buyertrx.setPersonId(user.getId());
			product.setId(tx.getId());
			product=ps.getProduct(product.getId());
			Date date=new Date();
			buyertrx.setBuyTime(date.getTime());
			buyertrx.setBuyPrice(product.getPrice());
			
			for(int i=0;i<tx.getNumber();i++){
				ts.insertTrx(buyertrx);
			}
			
		}
//		model.addAttribute("product",product);
		model.addAttribute("code",200);
	}
//	@RequestMapping("/add")
//	public String add(@PathVariable(value = "id") int id,
//			HttpServletRequest request, HttpServletResponse response) {
//		// 添加cookie
//		return ;
//	}
	@RequestMapping(value="/settleAccount")
	public String settleAccount(Model model,HttpServletRequest request){
		//购物车,怎样让点击购物车之后有反应
		Cookie[] cookies=request.getCookies();
		model.addAttribute("code",200);
		model.addAttribute("settleAccount",cookies);
		return"settleAccount";
	}
}
